/***************************************************************************
 *   Copyright (C) 2006 by Sergejs Melderis   *
 *   smelderis@yahoo.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "languagechooser.h"
#include <kiconloader.h>
#include <kglobal.h>
#include <qpixmap.h>
#include <qapplication.h>
#include <qpopupmenu.h>
#include <qlayout.h>
#include <klocale.h>
#include <qtoolbutton.h>
#include <qapplication.h>
#include "prefs.h"
#include <qpainter.h>
#include <qevent.h>
#include <qdrawutil.h>
// for clipboard translation
#include <qclipboard.h>

FlatButton::FlatButton(QWidget *parent, QPixmap *pix):
    QButton(parent, 0), pixmap(pix)
{
    setBackgroundOrigin( AncestorOrigin );
}

FlatButton::~FlatButton()
{
}

void FlatButton::drawButton(QPainter *p)
{
    p->fillRect(rect(), colorGroup().brush(QColorGroup::Background));
    drawButtonLabel(p);
}

void FlatButton::drawButtonLabel(QPainter *p)
{
    p->drawPixmap(0, 0, *pixmap);
}

void FlatButton::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    drawButton(&p);
}

LanguageLabel::LanguageLabel(QWidget *parent, const char *name):
    QLabel(parent, name)
{
    setFixedSize(17, 15);
    setPixmap(QPixmap::fromMimeSource(QString(name)));
}

LanguageLabel::~LanguageLabel()
{
}

void LanguageLabel::mousePressEvent(QMouseEvent *e)
{
    mMenu->popup(e->globalPos());
}

void LanguageLabel::changeName(const QString &name)
{
    if (name == this->name())
        return;
    setName(name.latin1());
    setPixmap(QPixmap::fromMimeSource(name));
}

void LanguageLabel::setPopupMenu(QPopupMenu *menu)
{
    mMenu = menu;
}

QPopupMenu* LanguageLabel::getMenu()const
{
    return mMenu;
}

LanguageChooser::LanguageChooser(
                                 const QString &from, const QString &to,
                                 QWidget *parent, const char *name)
    : QWidget(parent, name)
{
    mTranslateL = new QLabel(i18n("Translate:"), this);
    mTranslateL->setBackgroundOrigin(AncestorOrigin);
    QFont f(mTranslateL->font());
    f.setPixelSize(12);
    mTranslateL->setFont(f);

    mFromL = new LanguageLabel( this, from.latin1());
    KIconLoader *iconLoader = KGlobal::iconLoader();

    pmTranslateClipboard = new QPixmap();
    *pmTranslateClipboard = iconLoader->loadIcon("editpaste.png", KIcon::NoGroup, KIcon::SizeSmall);
    mTranslateClipboardB = new FlatButton(this, pmTranslateClipboard);
    mTranslateClipboardB->setBackgroundOrigin(AncestorOrigin);
    setBackgroundMode(X11ParentRelative);
    mTranslateClipboardB->setFixedSize(16, 16);

    pc = new QPixmap();
    *pc = iconLoader->loadIcon("reload.png", KIcon::NoGroup, KIcon::SizeSmall);
    mSwitchB = new FlatButton(this, pc);
    mSwitchB->setBackgroundOrigin(AncestorOrigin);
    setBackgroundMode(X11ParentRelative);
    mSwitchB->setFixedSize(16, 16);

    mToL = new LanguageLabel( this, to.latin1());

    mFromMenu = new QPopupMenu(this);
    mToMenu = new QPopupMenu(this);

    int i = 0;
    QStringList list = Prefs::languagesList();
    for (QStringList::iterator it = list.begin();it != list.end(); it++) {
        mFromMenu->insertItem(QIconSet(QPixmap::fromMimeSource(*it)), (*it), i);
        mToMenu->insertItem(QIconSet(QPixmap::fromMimeSource(*it)), (*it),i);

        i++;
    }

    QHBoxLayout *l = new QHBoxLayout();
    l->setMargin(0);
    l->addWidget(mTranslateL);
    l->insertSpacing(-1, 3);
    l->insertStretch(-1);
    l->addWidget(mTranslateClipboardB);
    l->insertSpacing(-1, 3);
    l->addWidget(mFromL);
    l->insertSpacing(-1, 3);
    l->addWidget(mSwitchB);
    l->insertSpacing(-1, 3);
    l->addWidget(mToL);
    l->insertSpacing(-1, 1);

    QVBoxLayout *tl = new QVBoxLayout(this);
    tl->setMargin(0);
    tl->addLayout(l);
    tl->insertSpacing(-1, 1);

    mToL->setPopupMenu(mToMenu);
    mFromL->setPopupMenu(mFromMenu);
    connect(mTranslateClipboardB, SIGNAL(pressed()), this, SLOT(translateClipboard()));
    connect(mSwitchB, SIGNAL(pressed()), this, SLOT(switchLabels()));
    connect(mFromMenu, SIGNAL(activated(int)), this, SLOT(fromMenuActivated(int)));
    connect(mToMenu, SIGNAL(activated(int)), this, SLOT(toMenuActivated(int)));
}


LanguageChooser::~LanguageChooser()
{
    delete pc;
    delete pmTranslateClipboard;
}

void LanguageChooser::fromMenuActivated(int id)
{
    QString text = mFromMenu->text(id);
    if(text == mFromL->name())
        return;
    mFromL->changeName(text);
    if (text == "Russian") {
        mToL->changeName(Prefs::preferredLanguage());
    } else {
        mToL->changeName("Russian");
    }
    emit languageChange(QString(mFromL->name()), QString(mToL->name()));
}

void LanguageChooser::toMenuActivated(int id)
{
    QString text = mToMenu->text(id);
    if (text == mToL->name())
        return;
    mToL->changeName(text);
    if (text == "Russian") {
        mFromL->changeName(Prefs::preferredLanguage());
    } else {
        mFromL->changeName("Russian");
    }
    emit languageChange(QString(mFromL->name()), QString(mToL->name()));
}

void LanguageChooser::translateClipboard()
{
    QClipboard *cb = QApplication::clipboard();

    QString text = cb->text(QClipboard::Selection);
    if (text.isNull() || text.stripWhiteSpace().isEmpty())
        text = cb->text(QClipboard::Clipboard);
    if (!text.isNull() && !text.stripWhiteSpace().isEmpty())
    {
        emit translateClipboard (QString(text));
    }
}

void LanguageChooser::switchLabels()
{
    QString name = mFromL->name();
    mFromL->changeName(mToL->name());
    mToL->changeName(name);
    emit languageChange(mFromL->name(), mToL->name());
}

QPopupMenu* LanguageChooser::getFromMenu()const
{
    return mFromL->getMenu();
}

QPopupMenu* LanguageChooser::getToMenu()const
{
    return mToL->getMenu();
}
#include "languagechooser.moc"
